package com.bwie.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bwie.domain.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
    /**
     * 使用乐观锁扣减库存
     * @param id 商品ID
     * @param version 版本号
     * @return 更新影响的行数
     */
    @Update("UPDATE t_goods SET available_stock = available_stock - 1, version = version + 1 " +
            "WHERE id = #{id} AND version = #{version} AND available_stock > 0")
    int reduceStockWithOptimisticLock(@Param("id") Long id,@Param("version") Integer version);

}